package com.miguo.framework.component;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.miguo.framework.R;


/**
 * File Names: BaseStatusView
 * Describe:
 * User: LinXiaoZhi(87100854@qq.com)
 * Date: 2015-07-26
 * Time: 20:07
 */
@SuppressWarnings("all")
public class BaseStatusView extends LinearLayout {

    private Context mContext;

    private Activity mActivity;

    private View mContentView;

    private Handler handler;

    private long interval = 2000;

    private final int SCROLL_WHAT = 0;

    private boolean isNetwork = true;

    private boolean isExit = false;

    public BaseStatusView(Context context) {
        this(context, null);
    }

    public BaseStatusView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        handler = new NetworkHandler();
        inflate(this.mContext, R.layout.layer_base_status, this);
        findView();
        initView();
    }

    private LinearLayout mNetworkError;
    private RelativeLayout mRefresh;

    private void findView() {
        mNetworkError = (LinearLayout) findViewById(R.id.network_error_ll);
        mRefresh = (RelativeLayout) findViewById(R.id.refresh_rl);
    }

    private void initView() {
    }

    public void setActivity(Activity activity) {
        this.mActivity = activity;
    }

    public void setContentView(View contentView) {
        mContentView = contentView;
    }

    public RelativeLayout getRefreshView() {
        return mRefresh;
    }

    public LinearLayout getNetworkView() {
        return mNetworkError;
    }

    public void setRefreshVisibility(int visibility) {
        if (isNetwork == false) {
            mRefresh.setVisibility(View.GONE);
            mContentView.setVisibility(View.GONE);
            mNetworkError.setVisibility(View.VISIBLE);
        } else {
            if (View.VISIBLE == visibility) {
                mRefresh.setVisibility(View.VISIBLE);
                mContentView.setVisibility(View.GONE);
                mNetworkError.setVisibility(View.GONE);
            } else if (View.GONE == visibility) {
                mRefresh.setVisibility(View.GONE);
                mContentView.setVisibility(View.VISIBLE);
                mNetworkError.setVisibility(View.GONE);
            }
        }
    }

    public void startCheck() {
        sendCheck();
    }

    public void stopCheck() {
        handler.removeMessages(SCROLL_WHAT);
    }

    public void exit(boolean exit) {
        if (exit == true) {
            mContentView.setVisibility(View.VISIBLE);
            mRefresh.setVisibility(View.GONE);
            mNetworkError.setVisibility(View.GONE);
        }
        isExit = exit;
    }

    private void sendCheck() {
        checkNetwork();
        handler.removeMessages(SCROLL_WHAT);
        handler.sendEmptyMessageDelayed(SCROLL_WHAT, interval);
    }

    private void checkNetwork() {
        boolean wifiNet = true;
        boolean mobNet = true;
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            NetworkInfo wifiNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            wifiNet = wifiNetInfo.isConnected();
            mobNet = mobNetInfo.isConnected();
            if (mobNet == false && wifiNet == false) {
                if (isNetwork == true) {
                    isNetwork = false;
                    mRefresh.setVisibility(View.GONE);
                    mContentView.setVisibility(View.GONE);
                    mNetworkError.setVisibility(View.VISIBLE);
                }
            } else {
                if (isNetwork == false) {
                    isNetwork = true;
                    mRefresh.setVisibility(View.VISIBLE);
                    mContentView.setVisibility(View.GONE);
                    mNetworkError.setVisibility(View.GONE);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        if (hasWindowFocus && isExit == false) {
            sendCheck();
            return;
        }
        stopCheck();
    }

    private class NetworkHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SCROLL_WHAT:
                    sendCheck();
                    break;
                default:
                    break;
            }
        }
    }

}